/** * dc.cpp; compiled by VC++6.0 * divide-conquer, compute the nearest pair of points **/ #include #include #include "math.h" /** *struct node, to store the vertice in the graph **/ struct Node { int x; int y; }; /**struct Dist * for return, store the distance and nodes adjacent to it **/ struct Dist { Node start; Node end; double value; }; int first_L;// the value of first L /**initG() *to init the input graph **/ void initG(Node *g,int n) { g[0].x=31; g[0].y=0; g[1].x=48; g[1].y=0; g[2].x=37; g[2].y=8; g[3].x=43; g[3].y=9; g[4].x=30; g[4].y=13; g[5].x=38; g[5].y=20; g[6].x=42; g[6].y=19; g[7].x=50; g[7].y=25; g[8].x=39; g[8].y=30; g[9].x=41; g[9].y=30; g[10].x=36; g[10].y=40; g[11].x=46; g[11].y=40; g[12].x=47; g[12].y=75; g[13].x=40; g[13].y=50; g[14].x=49; g[14].y=55; g[15].x=34; g[15].y=60; g[16].x=33; g[16].y=70; g[17].x=44; g[17].y=65; g[18].x=0; g[18].y=5; g[19].x=10; g[19].y=4; g[20].x=17; g[20].y=40; g[21].x=5; g[21].y=50; g[22].x=12; g[22].y=60; g[23].x=60; g[23].y=1; g[24].x=65; g[24].y=14; g[25].x=69; g[25].y=55; g[26].x=74; g[26].y=43; g[27].x=78; g[27].y=2; } void merge (Node *A, int a, Node *B, int b, Node *C,int direct) { int i,j,k; i = 0; j = 0; k = 0; while (i < a && j < b) { if(direct==0){ if (A[i].x <= B[j].x) { /* copy A[i] to C[k] and move the pointer i and k forward */ C[k] = A[i]; i++; k++; } else { /* copy B[j] to C[k] and move the pointer j and k forward */ C[k] = B[j]; j++; k++; } } else { if (A[i].y <= B[j].y) { /* copy A[i] to C[k] and move the pointer i and k forward */ C[k] = A[i]; i++; k++; } else { /* copy B[j] to C[k] and move the pointer j and k forward */ C[k] = B[j]; j++; k++; } } } /* move the remaining elements in A into C */ while (i < a) { C[k]= A[i]; i++; k++; } /* move the remaining elements in B into C */ while (j < b) { C[k]= B[j]; j++; k++; } } /** * merge_sort() * Sort array A with n integers, using merge-sort algorithm. **/ void merge_sort(Node *g, int left,int right,int direct) { int i; Node *A1, *A2; int n=right-left+1; int n1, n2; if (n < 2) return; /* the array is sorted when n=1.*/ /* divide A into two array A1 and A2 */ n1 = n / 2; /* the number of elements in A1 */ n2 = n - n1; /* the number of elements in A2 */ A1 = (Node*)malloc(sizeof(Node) * n1); A2 = (Node*)malloc(sizeof(Node) * n2); /* move the first n/2 elements to A1 */ for (i =0; i < n1; i++) { A1[i] = g[i]; } /* move the rest to A2 */ for (i = 0; i < n2; i++) { A2[i] = g[i+n1]; } /* recursive call */ merge_sort(A1, 1,n1,direct); merge_sort(A2,1,n2,direct); /* conquer */ merge(A1, n1, A2, n2, g,direct); free(A1); free(A2); } /**Swap() *swap two elements in the vector **/ void Swap(Node *node,int a,int b) { Node temp; temp=node[a]; node[a]=node[b]; node[b]=temp; } /** * Partion() *partion elements between low and high in the vector **/ int Partion(Node *g,int low, int high,int direct) { int pos=low; Node p=g[low]; for(int i=low+1;i<=high;i++){ if(direct==0){ if(g[i].x (%d,%d) ", new_g[k].x,new_g[k].y,temp,new_g[l].x,new_g[l].y); if(lL){ gy2[index2]=gy[i]; index2++; } } /* recursive call */ d1=Mini_dist(g1, n1,gy1); printf("----------------------\n"); printf("d1.value = %.2f", d1.value); printf("\n"); printf("----------------------\n"); d2=Mini_dist(g2, n2,gy2); printf("----------------------\n"); printf("d2.value = %.2f", d2.value); printf("\n"); printf("----------------------\n"); /* conquer */ mini=find_mini(d1, d2); printf("\n"); printf("------combine--------"); printf("----------------------\n"); d=combine(g,mini,L,n,gy); free(g1); free(g2); free(gy1); free(gy2); return d; } /** * main() * The main function for input and output **/ void main(int argv, char** args) { Node *g; Node *g_sorted_by_y; int i; Dist dist; int size;//size of the input graph size=28; g = (Node *)malloc(sizeof(Node) * size); g_sorted_by_y = (Node *)malloc(sizeof(Node) * size); initG(g,size); printf("\n"); printf("************** Input **************\n"); printf("\n"); printf("------------------------------\n"); printf("The input graph is: \n"); for (i = 0; i < size; i++) { printf("(%d, %d)", g[i].x,g[i].y); printf("\n"); } printf("------------------------------\n"); merge_sort(g,0,size-1,1); merge_sort(g,0,size-1,0); for(i=0;i "); printf("(%d, %d)", dist.end.x,dist.end.y); printf("\n"); printf("The distance is: %.2f\n", dist.value); printf("------------------------------\n"); free(g); free(g_sorted_by_y); printf("Press any key to continue"); getchar(); }